//
//  GoodsDetailsHeaderView.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/20.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodsDetailsHeaderView.h"

@interface GoodsDetailsHeaderView()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollV;

@property (nonatomic,strong) UILabel *pageLB;//   当前页数/总页数   LB
  
@property (nonatomic,strong) UILabel *currentPriceLB;//当前价格

@property (nonatomic,strong) UILabel *originalPriceLB;//原价

@property (nonatomic,strong) UILabel *lowestPriceLB;//最低价

@property (nonatomic,strong) UIButton *focusOnBtn;//是否关注

@property (nonatomic,strong) UILabel *goodsInfoLB;//商品信息LB

@property (nonatomic,strong) UIView *lineV;//分隔线

@property (nonatomic,strong) NSMutableArray *goodsImgs;//图片数组

@property (nonatomic,strong) UIView *priceLineV;

@end

@implementation GoodsDetailsHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.goodsImgs = [NSMutableArray array];
        [self initUI];
    }
    return self;
}

- (void)setCurrentSkuModel:(GoodsSkuModel *)currentSkuModel{
    _currentSkuModel = currentSkuModel;
    
    self.currentPriceLB.text = [Configuration getTotalPriceWithPrice:_currentSkuModel.price withTbPrice:_currentSkuModel.tbprice];
    self.currentPriceLB.width = [Configuration getLabelWidthWithText:self.currentPriceLB.text withFont:self.currentPriceLB.font];
    
    self.originalPriceLB.left = self.currentPriceLB.right + 5;
    self.originalPriceLB.text = [Configuration getTotalPriceWithPrice:_currentSkuModel.mprice withTbPrice:_currentSkuModel.tbprice];
    self.originalPriceLB.width = [Configuration getLabelWidthWithText:self.originalPriceLB.text withFont:self.originalPriceLB.font];
    
    self.priceLineV.width = self.originalPriceLB.width;
    
    self.originalPriceLB.hidden = YES;
    self.priceLineV.hidden = YES;
}

#pragma mark - setter
- (void)setModel:(GoodsDetailModel *)model{
    _model = model;
    self.goodsImgs = [NSMutableArray arrayWithArray:_model.detailpic];
    self.pageLB.text = [NSString stringWithFormat:@"1/%ld",(long)self.goodsImgs.count];
    for (UIView *view in self.scrollV.subviews) {
        [view removeFromSuperview];
    }
    self.scrollV.contentSize = CGSizeMake(ScreenWidth*self.goodsImgs.count, ScreenWidth);
    for (int i = 0; i<self.goodsImgs.count; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(i*ScreenWidth, 0, ScreenWidth, ScreenWidth)];
        [image sd_setImageWithURL:[NSURL URLWithString:self.goodsImgs[i]] placeholderImage:LoadImage(@"")];
        image.contentMode = UIViewContentModeScaleToFill;
        [self.scrollV addSubview:image];
    }
    
    self.goodsInfoLB.text = _model.name;
    self.lowestPriceLB.text = _model.label;
    
    self.goodsInfoLB.height = [Configuration getLabelHeightWithText:self.goodsInfoLB.text withFont:self.goodsInfoLB.font withLBWidth:self.goodsInfoLB.width];
    self.lineV.top = self.lineV.bottom + 20;
    self.height = self.lineV.bottom;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.updateHeightBlock) {
            self.updateHeightBlock(self.height);
        }
    });
}
#pragma mark - UI
-(void)initUI{
    [self addSubview:self.scrollV];
    [self addSubview:self.pageLB];
    [self addSubview:self.currentPriceLB];
    [self addSubview:self.originalPriceLB];
    [self.originalPriceLB addSubview:self.priceLineV];
    [self addSubview:self.lowestPriceLB];
    [self addSubview:self.focusOnBtn];
    [self addSubview:self.goodsInfoLB];
    
    [self addSubview:self.lineV];
    self.height = self.lineV.bottom;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.updateHeightBlock) {
            self.updateHeightBlock(self.height);
        }
    });
}

#pragma mark - lazy
- (UIScrollView *)scrollV{
    if (!_scrollV) {
        _scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth)];
        _scrollV.contentSize = CGSizeMake(ScreenWidth, ScreenWidth);
        _scrollV.pagingEnabled = YES;
        _scrollV.bounces = NO;
        _scrollV.delegate = self;
    }
    return _scrollV;
}
//商品介绍图片   页数
- (UILabel *)pageLB{
    if (!_pageLB) {
        _pageLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-64, ScreenWidth-43, 45, 24)];
        _pageLB.clipsToBounds = YES;
        _pageLB.layer.cornerRadius = 12;
        _pageLB.backgroundColor = RGBA(0, 0, 0, 0.3f);
        _pageLB.textColor = UIColor.whiteColor;
        _pageLB.textAlignment = NSTextAlignmentCenter;
        _pageLB.font = FontSet(12);
        _pageLB.text = @"1/1";
    }
    return _pageLB;
}
//当前价格lb
- (UILabel *)currentPriceLB{
    if (!_currentPriceLB) {
        _currentPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(15, self.scrollV.bottom+25, 80, 24)];
        _currentPriceLB.textColor = RGB(255, 34, 21);
        _currentPriceLB.font = FontSet(18);
        _currentPriceLB.text = @"¥0";
        _currentPriceLB.width = [self getLabelWidthWithLB:_currentPriceLB];
    }
    return _currentPriceLB;
}
//原价lb
- (UILabel *)originalPriceLB{
    if (!_originalPriceLB) {
        _originalPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(self.currentPriceLB.right+5, self.currentPriceLB.top+6, 120, 18)];
        _originalPriceLB.textColor = MainGrayTextColor;
        _originalPriceLB.font = FontSet(13);
        _originalPriceLB.text = @"¥0";
        _originalPriceLB.width = [self getLabelWidthWithLB:_originalPriceLB];
    }
    return _originalPriceLB;
}
- (UIView *)priceLineV{
    if (!_priceLineV) {
        _priceLineV = [[UIView alloc] initWithFrame:CGRectMake(0, 8, self.originalPriceLB.width, 1)];
        _priceLineV.backgroundColor = MainGrayTextColor;
    }
    return _priceLineV;
}

//最低价 时段
- (UILabel *)lowestPriceLB{
    if (!_lowestPriceLB) {
        _lowestPriceLB = [[UILabel alloc] initWithFrame:CGRectMake(15, self.currentPriceLB.bottom+7, ScreenWidth-30, 16)];
        _lowestPriceLB.textColor = RGB(220, 96, 85);
        _lowestPriceLB.font = FontSet(11);
        _lowestPriceLB.text = @"本月最低价";
    }
    return _lowestPriceLB;
}
//设置收藏 btn
- (UIButton *)focusOnBtn{
    if (!_focusOnBtn) {
        _focusOnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _focusOnBtn.frame = CGRectMake(ScreenWidth-45, self.scrollV.bottom+22, 35, 35);
        [_focusOnBtn setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
        [_focusOnBtn addTarget:self action:@selector(setFocusClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _focusOnBtn;
}

//商品信息
- (UILabel *)goodsInfoLB{
    if (!_goodsInfoLB) {
        _goodsInfoLB = [[UILabel alloc] initWithFrame:CGRectMake(15, self.lowestPriceLB.bottom+10, ScreenWidth-30, 50)];
        _goodsInfoLB.textColor = MainBlackTextColor;
        _goodsInfoLB.font = FontSet(16);
        _goodsInfoLB.numberOfLines = 0;
        _goodsInfoLB.text = @"";
        _goodsInfoLB.height = [Configuration getLabelHeightWithText:_goodsInfoLB.text withFont:_goodsInfoLB.font withLBWidth:_goodsInfoLB.width];
    }
    return _goodsInfoLB;
}
- (UIView *)lineV{
    if (!_lineV) {
        _lineV = [[UIView alloc] initWithFrame:CGRectMake(0, self.goodsInfoLB.bottom+20, ScreenWidth, 12)];
        _lineV.backgroundColor = MainBackColor;
    }
    return _lineV;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger count = (NSInteger)(scrollView.contentOffset.x/ScreenWidth);
    self.pageLB.text = [NSString stringWithFormat:@"%ld/%ld",(long)count+1,self.goodsImgs.count];
}
//设置收藏
-(void)setFocusClick{
    
}

//获取lb宽度
-(float)getLabelWidthWithLB:(UILabel *)lb{
    return [Configuration getLabelWidthWithText:lb.text withFont:lb.font];
}



@end




